package com.cn.pet.controller;

import com.cn.pet.model.pojo.Brand;
import com.cn.pet.model.pojo.Keyword;
import com.cn.pet.service.BrandService;
import com.cn.pet.service.KeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class KeywordController {

    @Autowired
    KeywordService keywordService;

    @Autowired
    BrandService brandService;

    // 查询
    @RequestMapping(value = "/keywords", method = RequestMethod.GET)
    public String listAllKeywords(Model model) {
        model.addAttribute("keywordList", keywordService.findKeywordByCondition(new HashMap<>()));
        return "keyword_list";
    }

    // 查询
    @RequestMapping(value = "/keywords", method = RequestMethod.POST)
    public String listKeywords(@RequestParam("brandSelect") Integer brandId, Model model) {
        Map<String,Object> paramMap = new HashMap<>();
        if(brandId != 0) {
            paramMap.put("brandId",brandId);
            model.addAttribute("brandId",brandId);
        }
        List<Keyword> keywordList = keywordService.findKeywordByCondition(paramMap);
        model.addAttribute("keywordList", keywordList);
        return "keyword_list";
    }

    // 新增修改
    @ResponseBody
    @RequestMapping(value = "/keyword", method = RequestMethod.POST)
    public String addOrUpdateKeyword(@RequestBody Keyword keyword) {
        return keywordService.addOrUpdateKeyword(keyword);
    }

    //删除
    @ResponseBody
    @RequestMapping(value = "/keyword/delete/id/{id}", method = RequestMethod.GET)
    public String deleteKeyword(@PathVariable("id") int id){
        return keywordService.deleteKeyword(id);
    }

    @ResponseBody
    @RequestMapping(value = "/keyword/brandId/{brandId}", method = RequestMethod.GET)
    public List<Keyword> getKeywordByBrandAjax(@PathVariable("brandId") int brandId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("brandId",brandId);
        return keywordService.findKeywordByCondition(paramMap);
    }

    @ResponseBody
    @RequestMapping(value = "/keyword/id/{id}", method = RequestMethod.GET)
    public List<Keyword> getKeywordByIdAjax(@PathVariable("id") int id){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",id);
        return keywordService.findKeywordByCondition(paramMap);
    }

}
